//
//  ViewController.m
//  时钟
//
//  Created by 黄星 on 16/1/14.
//  Copyright © 2016年 qingfanqie. All rights reserved.
//

#import "ViewController.h"
#import "UIColor+ColorPlus.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@property (nonatomic, strong) UIView *vSecond;

@property (nonatomic, strong) UIView *vMinute;

@property (nonatomic, strong) UIView *vHour;

@property (nonatomic, strong) UIButton *btn;

@property (nonatomic, assign) CGFloat angle;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, strong) NSTimer *timer2;

@property (nonatomic, assign) CGFloat currentSec;

@property (nonatomic, assign) BOOL isCheck;
@end

@implementation ViewController

#warning 自定义导航
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        UILabel *label =  [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
        label.text = @"标题";
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor blueColor];
        self.navigationItem.titleView = label;

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.angle = 0;
    self.currentSec = 0;
    [self initBg];
    [self initView];
    
}

- (void)initBg
{
    UIView *vBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 128, ScreenHeight / 2 - 128, 256, 256)];
    vBg.backgroundColor = [UIColor lightBlueColor];
    vBg.layer.borderColor = [UIColor chocolateColor].CGColor;
    vBg.layer.borderWidth = 1;
    vBg.layer.masksToBounds = YES;
    vBg.layer.cornerRadius = 128;
    [self.view addSubview:vBg];
    
    for (NSInteger i = 0; i < 12; i++) {
        UILabel *lab = [[UILabel alloc] init];
        lab.text = [NSString stringWithFormat:@"%@",@(i == 0 ? 12:i)];
        lab.textColor = [UIColor fleshColor];
        lab.font = [UIFont boldSystemFontOfSize:20];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.layer.anchorPoint = CGPointMake(0.5, 1);
        lab.frame = CGRectMake(ScreenWidth / 2 - 12, ScreenHeight / 2 - 220, 24, 220);
        //(i / 12.0) * M_PI * 2.0;
        lab.transform = CGAffineTransformMakeRotation((i / 12.0) * M_PI * 2.0);
        [self.view addSubview:lab];
    }
    
}
/**
 360 / 60
    6
 */

- (void)initView
{
    
    NSLog(@"%@",NSStringFromCGRect(self.vSecond.frame));
    
    self.vHour = [[UIView alloc] init];
    self.vHour.backgroundColor = [UIColor fireBrickRedColor];
    self.vHour.layer.anchorPoint = CGPointMake(1, 1);
    self.vHour.frame = CGRectMake(ScreenWidth / 2, ScreenHeight / 2 - 70, 2.2, 70);
    [self.view addSubview:self.vHour];
    
    self.vMinute = [[UIView alloc] init];
    self.vMinute.backgroundColor = [UIColor fireBrickRedColor];
    self.vMinute.layer.anchorPoint = CGPointMake(1, 1);
    self.vMinute.frame = CGRectMake(ScreenWidth / 2 - 1, ScreenHeight / 2 - 95, 2, 95);
    [self.view addSubview:self.vMinute];
    
    self.vSecond = [[UIView alloc] init];
    NSLog(@"%@",NSStringFromCGRect(self.vSecond.frame));
    
    self.vSecond.backgroundColor = [UIColor maroonRedColor];
    self.vSecond.layer.anchorPoint = CGPointMake(1, 1);
    self.vSecond.frame = CGRectMake(ScreenWidth / 2, ScreenHeight / 2 - 100, 2, 100);
    [self.view addSubview:self.vSecond];

    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(80, 84, 100, 30);
    self.btn.backgroundColor = [UIColor redColor];
    [self.btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];

    UIView *vBG = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth / 2 - 5, ScreenHeight / 2 - 5, 10, 10)];
    vBG.layer.cornerRadius = 5;
    vBG.layer.masksToBounds = YES;
    vBG.backgroundColor = [UIColor fireBrickRedColor];
    [self.view addSubview:vBG];

    [self openTimer2];

}

- (void)click
{
    if (self.isCheck) {
        //启动定时器
//        self.timer.fireDate = [NSDate distantPast];
        //停止定时器
        self.timer2.fireDate = [NSDate distantFuture];

        [self openTimer];
   } else {
       //启动定时器
//       self.timer2.fireDate = [NSDate distantPast];
       //停止定时器
       self.timer.fireDate = [NSDate distantFuture];

       [self openTimer2];
       
    }
    
    self.isCheck = !self.isCheck;
}

- (void)openTimer
{
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(start) userInfo:nil repeats:YES];
    } else {
        [self.timer setFireDate:[NSDate distantPast]];
    }
}

- (void)openTimer2
{
    if (!self.timer2) {
        self.timer2 = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(start2) userInfo:nil repeats:YES];
    } else {
        [self.timer2 setFireDate:[NSDate distantPast]];
    }
}


- (void)start2
{

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
    CGFloat fHours = (components.hour / 12.0) * M_PI * 2.0;
    CGFloat fMins = (components.minute / 60.0) * M_PI * 2.0;
    CGFloat fSecs = (components.second / 60.0) * M_PI * 2.0;

    NSString *str = [NSString stringWithFormat:@"%.2f",fSecs];
    NSLog(@"fSec = %@, sec = %@",str, @(components.second));
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    basicAnimation.fromValue = [NSNumber numberWithFloat:self.currentSec];
    basicAnimation.toValue = [NSNumber numberWithFloat:fSecs];
    basicAnimation.fillMode = kCAFillModeForwards;
    basicAnimation.removedOnCompletion = NO;
    basicAnimation.repeatCount = 0;
    basicAnimation.duration = 1;
    if ([str isEqualToString:@"0.00"]) {
        basicAnimation.toValue = [NSNumber numberWithFloat:6.28];
        self.currentSec = 0.00;
    } else {
        basicAnimation.toValue = [NSNumber numberWithFloat:fSecs];
        self.currentSec = fSecs;
    }
    
    [self.vSecond.layer addAnimation:basicAnimation forKey:@"transform.rotation.z"];

    self.vMinute.transform = CGAffineTransformMakeRotation(fMins);
    self.vHour.transform = CGAffineTransformMakeRotation(fHours);
    [self.btn setTitle:[NSString stringWithFormat:@"%ld:%ld:%ld", components.hour, components.minute, components.second] forState:UIControlStateNormal];

}

- (void)start
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
    CGFloat fHours = (components.hour / 12.0) * M_PI * 2.0;
    CGFloat fMins = (components.minute / 60.0) * M_PI * 2.0;
    CGFloat fSecs = (components.second / 60.0) * M_PI * 2.0;

    self.vSecond.transform = CGAffineTransformMakeRotation(fSecs);
    self.vMinute.transform = CGAffineTransformMakeRotation(fMins);
    self.vHour.transform = CGAffineTransformMakeRotation(fHours);
    [self.btn setTitle:[NSString stringWithFormat:@"%ld:%ld:%ld", components.hour, components.minute, components.second] forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
